using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories;
using Core.Toolkit.Common;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Application;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Application
{
    [AutoDi()]
    public class FX_MapPositionService : BaseAppService<FX_MapPositionDto, FX_MapPosition, FX_MapPositionRepository>
    {
        readonly ExcelHelp _excelHelp;
        public FX_MapPositionService(FX_MapPositionRepository repository, ExcelHelp excelHelp) : base(repository)
        {
            _excelHelp = excelHelp;
        }



        private LoadPageOffsetModelAuto<FX_MapPosition, FX_MapPositionDto>
            GetLoadPageOffsetMode(PagingModel<FX_MapPositionDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = this.GetLoadPageOffsetModelAuto(pagingQueryModel);
            var baseQuery = _repository.QueryBase().AsNoTracking();
            loadPageOffsetModel.QueryWhere = this.CreateQRList()

     .Add("Name", "=") //
     .Add("SysAreaId", "=") //区域ID
     .Add("X", "=") //X
     .Add("Y", "=") //Y
     .Add("SubFactory", "=");//隶属工厂


            loadPageOffsetModel.BasqQuery = baseQuery;
            loadPageOffsetModel.Order = orderModel => orderModel.CreateTime;
            loadPageOffsetModel.Selector = model => new FX_MapPosition()
            {
                Id = model.Id,
                Name = model.Name,//
                SysAreaId = model.SysAreaId,//区域ID
                X = model.X,//X
                Y = model.Y,//Y
                SubFactory = model.SubFactory,//隶属工厂
            };
            return loadPageOffsetModel;
        }


        public async Task<IPageModel<FX_MapPositionDto>> GetList(PagingModel<FX_MapPositionDto>
            pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            return await base.LoadPageOffsetAsync(loadPageOffsetModel);
        }


        public async Task<string>
            Export(PagingModel<FX_MapPositionDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            var DataList = await this.ExportDataAsync(loadPageOffsetModel);
            var excelData = DataList.Select(model => new
            {
                model.Name,
                Name_title = "",
                model.SysAreaId,
                SysAreaId_title = "区域ID",
                model.X,
                X_title = "X",
                model.Y,
                Y_title = "Y",
                model.SubFactory,
                SubFactory_title = "隶属工厂",
            }).ToList();




            return _excelHelp.Create(excelData);
        }


        public Task<List<FX_MapPosition>> GetAllAsync()
        {
            return _repository.QueryBase().ToListAsync();
        }
    }

}
